package com.dmap.run.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.run.bo.ClimbingRecordSimple;
import com.dmap.run.bo.RunRecordSimple;
import com.dmap.run.constants.RunConstans;
import com.dmap.run.controller.api.param.ClimbingRecordParam;
import com.dmap.run.controller.api.param.RunRecordParam;
import com.dmap.run.po.ClimbingRecord;
import com.dmap.run.po.RunRecord;
import com.dmap.run.po.mongo.MapTrack;
import com.dmap.run.service.ClimbingRecordService;
import com.dmap.run.service.MapTrackService;
import com.dmap.run.service.RunRecordService;
import com.dmap.run.vo.ClimbingRecordDetailVo;
import com.dmap.run.vo.ClimbingRecordHistoryVo;
import com.dmap.run.vo.RunRecordDetailVo;
import com.dmap.run.vo.RunRecordHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * \* User: silent
 * \* Date: 08/06/18 Time: 上午 10:33
 * \* Description: 登山相关接口
 * \
 */

@Api(value = "登山相关接口", description = "登山相关接口")
@Controller
@RequestMapping(value = "/api/dmap-run-server/v1/climbing")
public class ClimbingRecordController {


    @Resource
    private ClimbingRecordService climbingRecordService;
    @Resource
    private MapTrackService mapTrackService;


    @ResponseBody
    @ApiOperation(value = "接受登山数据", notes = "接受登山数据")
    @RequestMapping(value = "/accept/climbing/data", method = RequestMethod.POST)
    public BaseVo accpectClimbingRecord(@RequestBody ClimbingRecordParam param) {
        BaseVo baseVo = new BaseVo();
        if (param.getUserId() == null || param.getType() == null
                || param.getDataSource() == null || param.getStartTime() == null
                || param.getEndTime() == null || param.getDistanceTotal() == null
                || param.getCalorie() == null
                || param.getStepNumTotal() == null || param.getTimeTotal() == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (climbingRecordService.isExistClimbingRecord(param.getUserId(), param.getStartTime(), param.getEndTime())) {
            baseVo.setCode(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        boolean isSuccess = climbingRecordService.insertClimbingRecord(param);
        if (!isSuccess) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        if (CollectionUtils.isNotEmpty(param.getTrackList())) {
            MapTrack mapTrack = new MapTrack();
            mapTrack.setUserId(param.getUserId());
            mapTrack.setType(3);
            mapTrack.setDataSource(param.getDataSource());
            mapTrack.setStartTime(param.getStartTime());
            mapTrack.setEndTime(param.getEndTime());
            mapTrack.setTrackList(param.getTrackList());
            mapTrackService.insertMapTrack(mapTrack);
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取用户登山历史总公里数", notes = "获取用户历史登山总公里数")
    @RequestMapping(value = "/get/user/total/distance", method = RequestMethod.GET)
    public BaseVo getUserDistanceTotal(@RequestParam Long userId) {
        BaseVo baseVo = new BaseVo();
        if (userId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        int totalDistance = climbingRecordService.selectUserTotalDistance(userId);
        Map<String, Integer> map = new HashMap<>();
        map.put("totalDistance", totalDistance);
        baseVo.setData(map);
        return baseVo;
    }


    @ResponseBody
    @ApiOperation(value = "获取历史登山数据列表", notes = "获取历史登山数据列表")
    @RequestMapping(value = "/get/climbing/data", method = RequestMethod.GET)
    public BaseVo getClimbingRecordList(@RequestParam Long userId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        BaseVo baseVo = new BaseVo();
        if (userId == null || pageIndex == null || pageSize == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        ClimbingRecordHistoryVo runRecordHistoryVo = climbingRecordService.selectClimbingRecordHistoryList(userId);
        if (runRecordHistoryVo.getClimbingNum() > 0) {
            List<ClimbingRecord> climbingRecords = climbingRecordService.selectClimbingRecordListByPage(userId, pageIndex, pageSize);
            List<ClimbingRecordSimple> runRecordSimpleList = new ArrayList<>(pageSize);
            for (ClimbingRecord record : climbingRecords) {
                runRecordSimpleList.add(new ClimbingRecordSimple(record));
            }
            runRecordHistoryVo.setClimbingRecordSimpleList(runRecordSimpleList);
        }
        baseVo.setData(runRecordHistoryVo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取登山数据详情", notes = "获取登山数据详情")
    @RequestMapping(value = "/accept/climbing/data/detail", method = RequestMethod.GET)
    public BaseVo getClimbingRecordDetail(@RequestParam Long climbingRecordId) {
        BaseVo baseVo = new BaseVo();
        if (climbingRecordId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        ClimbingRecord climbingRecord = climbingRecordService.selectClimbingRecordById(climbingRecordId);
        //没有查到返回错误码
        if (climbingRecord == null) {
            baseVo.setCode(RunConstans.RESPONSE_NOT_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_NOT_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        ClimbingRecordDetailVo recordDetailVo = new ClimbingRecordDetailVo(climbingRecord);
        //获取地图轨迹数据
        MapTrack mapTrack = mapTrackService.selectMapTrackByUserIdAndStartTimeAndEndTime(climbingRecord.getUserId(), climbingRecord.getStartTime(), climbingRecord.getEndTime());
        if (mapTrack != null) {
            recordDetailVo.setTrackList(mapTrack.getTrackList());
        } else {
            //没有地图轨迹放回一个空集合
            recordDetailVo.setTrackList(Collections.emptyList());
        }
        baseVo.setData(recordDetailVo);
        return baseVo;
    }

}
